<html>
<head><meta charset="utf-8"><title>return value from proc macro · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html">return value from proc macro</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205310682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205310682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205310682">(Jul 29 2020 at 01:56)</a>:</h4>
<p>I'm learning about proc macros, and I want to write one that essentially takes in a string at compile time, calls a function on it, and returns the result of the function to the calling code (so it's like calling the function at compile-time). Is there a way to do that with proc macros?</p>



<a name="205321021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205321021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205321021">(Jul 29 2020 at 05:59)</a>:</h4>
<p>Emit the tokens that create a literal of the return value.</p>



<a name="205546277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205546277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205546277">(Jul 31 2020 at 00:04)</a>:</h4>
<p><span class="user-mention" data-user-id="296643">@isHavvy</span> Is there a way to get <code>proc_macro</code> to convert a struct (or enum) to its literal token representation?</p>



<a name="205570526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205570526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205570526">(Jul 31 2020 at 08:53)</a>:</h4>
<p>Nothing I know of personally, but you can always make a trait for it.</p>



<a name="205617930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205617930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205617930">(Jul 31 2020 at 17:29)</a>:</h4>
<p>How would you go about making a trait for it? (I'm pretty new to proc macros)</p>



<a name="205672927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205672927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205672927">(Aug 01 2020 at 09:10)</a>:</h4>
<p>If you have lots and lots of these structs, you'd probably want to implement a derive macro that generates a <code>ToTokens</code> implementation. Otherwise, you're likely better off writing out the structure yourself using <code>quote</code>.</p>



<a name="205690935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205690935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205690935">(Aug 01 2020 at 17:39)</a>:</h4>
<p><span class="user-mention" data-user-id="326186">@Sergio Benitez</span> Thanks for your help! I actually have just one struct (<code>regex::Regex</code>) that I need to generate at compile time. I'll try out <code>quote</code>!</p>



<a name="205691565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205691565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205691565">(Aug 01 2020 at 17:54)</a>:</h4>
<p>Although it looks like I might not be able to do it because the field in <code>regex::Regex</code> is private.</p>



<a name="205695948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/return%20value%20from%20proc%20macro/near/205695948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/return.20value.20from.20proc.20macro.html#205695948">(Aug 01 2020 at 19:58)</a>:</h4>
<p>If you're trying to compile regex at compile-time, look at <a href="https://github.com/rust-lang/regex/issues/607">https://github.com/rust-lang/regex/issues/607</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>